package edu.cuit.avatar;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 函数式接口:有且仅有一个抽象方法的接口
 * java内置的函数式接口
 *      Consumer<T>: 接收T类型的参数,没有返回值
 *      BiConsumer<T,U>: 接收T类型及U类型的参数, 没有返回值
 *      Function<T,R>: 接收T类型的参数，返回R类型结果
 *      Supplier<T>: 没有参数，返回T类型的结果
 *      Predicate<T>: 接收T类型的参数，返回boolean类型结果
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/11 9:05
 */
public class FunctionalInterfaceTest {
    public static void main(String[] args) {
        IService service = new IService() {
            @Override
            public void test() {
                System.out.println("匿名内部类");
            }
        };

        IService service2 = () -> System.out.println("λ表达式");
        service2.test();

        System.out.println("================================================");
        consumer(e -> System.out.println(e.length()));
        Function<String, Integer> function = new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return s.length();
            }
        };

        System.out.println(function.apply("welcome to java!"));

        Function<String, Integer> fun = s -> s.length();
        System.out.println(fun.apply("welcome to java!"));

        Predicate<String> predicate = s -> s.length() >= 4 && s.length() <= 12;
        System.out.println(predicate.test("peppa"));
    }

    public static void consumer(Consumer<String> consumer){
        consumer.accept("Hello");
    }
}
